package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.TExamSite;
import com.ruoyi.system.service.ITExamSiteService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TExamRoom;
import com.ruoyi.system.service.ITExamRoomService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 考场Controller
 * 
 * @author ruoyi
 * @date 2021-03-02
 */
@Controller
@RequestMapping("/system/room")
public class TExamRoomController extends BaseController
{
    private String prefix = "system/room";

    @Resource
    private ITExamRoomService tExamRoomService;

    @Resource
    private ITExamSiteService examSiteService;

    @RequiresPermissions("system:room:view")
    @GetMapping()
    public String room()
    {
        return prefix + "/room";
    }

    /**
     * 查询考场列表
     */
    @RequiresPermissions("system:room:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TExamRoom tExamRoom)
    {
        startPage();
        List<TExamRoom> list = tExamRoomService.selectTExamRoomList(tExamRoom);
        return getDataTable(list);
    }

    @PostMapping("/listSelect")
    @ResponseBody
    public AjaxResult listSelect(TExamRoom tExamRoom) {
        List<TExamRoom> list = tExamRoomService.selectTExamRoomList(tExamRoom);
        return AjaxResult.success(list);
    }

    /**
     * 导出考场列表
     */
    @RequiresPermissions("system:room:export")
    @Log(title = "考场", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TExamRoom tExamRoom)
    {
        List<TExamRoom> list = tExamRoomService.selectTExamRoomList(tExamRoom);
        ExcelUtil<TExamRoom> util = new ExcelUtil<TExamRoom>(TExamRoom.class);
        return util.exportExcel(list, "room");
    }

    /**
     * 新增考场
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<TExamSite> sites = examSiteService.selectTExamSiteList(new TExamSite());
        modelMap.put("sites",sites);
        return prefix + "/add";
    }

    /**
     * 新增保存考场
     */
    @RequiresPermissions("system:room:add")
    @Log(title = "考场", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TExamRoom tExamRoom)
    {
        return toAjax(tExamRoomService.insertTExamRoom(tExamRoom));
    }

    /**
     * 修改考场
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TExamRoom tExamRoom = tExamRoomService.selectTExamRoomById(id);
        Integer siteId = tExamRoom.getExamSite();
        TExamSite tExamSite = examSiteService.selectTExamSiteById(Long.valueOf(siteId));
        List<TExamSite> sites = examSiteService.selectTExamSiteList(new TExamSite());
        mmap.put("sites",sites);
        mmap.put("tExamRoom", tExamRoom);
        mmap.put("tExamSite", tExamSite);
        return prefix + "/edit";
    }

    /**
     * 修改保存考场
     */
    @RequiresPermissions("system:room:edit")
    @Log(title = "考场", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TExamRoom tExamRoom)
    {
        return toAjax(tExamRoomService.updateTExamRoom(tExamRoom));
    }

    /**
     * 删除考场
     */
    @RequiresPermissions("system:room:remove")
    @Log(title = "考场", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tExamRoomService.deleteTExamRoomByIds(ids));
    }
}
